package com.kwan.widget.indicator;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kwan.widget.R;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.List;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;


public class InitMagicIndicator {

	public static void init(Context mContext,
							MagicIndicator magicIndicator,
							ViewPager2 viewPage,
							List<NavigatorEntity> entities,
							NavigatorAdapter.NavigatorListener listener,
							boolean isAdjustMode,
							int selectedColor,int normalColor) {

		CommonNavigator commonNavigator = new CommonNavigator(mContext);
		commonNavigator.setAdjustMode(isAdjustMode);
		commonNavigator.setScrollPivotX(0.35f);

		NavigatorAdapter navigatorAdapter = new NavigatorAdapter(entities,selectedColor,normalColor);

		navigatorAdapter.setListener(new NavigatorAdapter.NavigatorListener() {
			@Override
			public void onClick(int index) {
				viewPage.setCurrentItem(index);
			}
		});


		commonNavigator.setAdapter(navigatorAdapter);
		magicIndicator.setNavigator(commonNavigator);

		viewPage.setAdapter(new ScreenSlidePageAdapter((FragmentActivity) mContext).setFragmentList(entities));

		viewPage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				super.onPageScrolled(position, positionOffset, positionOffsetPixels);
				magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
			}

			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
				listener.onClick(entities.get(position).getType());
				magicIndicator.onPageSelected(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {
				super.onPageScrollStateChanged(state);
				magicIndicator.onPageScrollStateChanged(state);
			}
		});

	}


	public void initBottom(Context context, MagicIndicator magicIndicator, ViewPager2 viewPager,
						   List<NavigatorEntity> entities) {

		magicIndicator.setBackgroundColor(Color.BLACK);
		CommonNavigator commonNavigator = new CommonNavigator(context);
		commonNavigator.setAdjustMode(true);
		commonNavigator.setAdapter(new CommonNavigatorAdapter() {

			@Override
			public int getCount() {
				return entities.size();
			}

			@Override
			public IPagerTitleView getTitleView(Context context, final int index) {
				CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);

				// load custom layout
				View customLayout = LayoutInflater.from(context).inflate(R.layout.simple_pager_title_layout, null);
				final ImageView titleImg = customLayout.findViewById(R.id.title_img);
				final TextView titleText = customLayout.findViewById(R.id.title_text);
				titleImg.setImageResource(entities.get(index).getImageSrc());
				titleText.setText(entities.get(index).getTitle());
				commonPagerTitleView.setContentView(customLayout);
				commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {

					@Override
					public void onSelected(int index, int totalCount) {
						titleText.setTextColor(context.getColor(R.color.colorAccent));
						titleImg.setColorFilter(context.getColor(R.color.colorAccent));
					}

					@Override
					public void onDeselected(int index, int totalCount) {
						titleText.setTextColor(Color.LTGRAY);
						titleImg.setColorFilter(Color.LTGRAY);
					}

					@Override
					public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
						//Timber.e("onLeave:" + index + titleImg);
						titleImg.setScaleX(1.3f + (1.0f - 1.3f) * leavePercent);
						titleImg.setScaleY(1.3f + (1.0f - 1.3f) * leavePercent);
					}

					@Override
					public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
						//Timber.e("onEnter:" + index + titleImg);
						titleImg.setScaleX(1.0f + (1.3f - 1.0f) * enterPercent);
						titleImg.setScaleY(1.0f + (1.3f - 1.0f) * enterPercent);
					}
				});

				commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						viewPager.setCurrentItem(index, true);
					}
				});

				return commonPagerTitleView;
			}

			@Override
			public IPagerIndicator getIndicator(Context context) {
				return null;
			}
		});
		magicIndicator.setNavigator(commonNavigator);

		viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				super.onPageScrolled(position, positionOffset, positionOffsetPixels);
				//Timber.e("viewPager onPageScrolled....");
				magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
			}

			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
				//Timber.e("viewPager onPageScrolled....");
				magicIndicator.onPageSelected(position);
			}



			@Override
			public void onPageScrollStateChanged(int state) {
				super.onPageScrollStateChanged(state);
				magicIndicator.onPageScrollStateChanged(state);
			}
		});

		viewPager.setAdapter(new ScreenSlidePageAdapter((FragmentActivity) context).setFragmentList(entities));
	}
}
